package com.ruoyi.category.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.category.mapper.SysPartMapper;
import com.ruoyi.category.domain.SysPart;
import com.ruoyi.category.service.ISysPartService;

/**
 * 配件Service业务层处理
 * 
 * @author YJF
 */
@Service
public class SysPartServiceImpl implements ISysPartService 
{
    @Autowired
    private SysPartMapper sysPartMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询配件
     * 
     * @param partId 配件主键
     * @return 配件
     */
    @Override
    public SysPart selectSysPartByPartId(Long partId)
    {
        return sysPartMapper.selectSysPartByPartId(partId);
    }

    /**
     * 查询配件列表
     * 
     * @param sysPart 配件
     * @return 配件
     */
    @Override
    public List<SysPart> selectSysPartList(SysPart sysPart)
    {
        return sysPartMapper.selectSysPartList(sysPart);
    }

    /**
     * 新增配件
     * 
     * @param sysPart 配件
     * @return 结果
     */
    @Override
    public int insertSysPart(SysPart sysPart)
    {
        //如果没有输入别名就直接用原名称
        String dictLabel="";
        if(StringUtils.isBlank(sysPart.getPartName())){
            dictLabel = dictDataMapper.selectDictLabel("sys_part_unit_type", sysPart.getUnitId());
            sysPart.setPartName(dictLabel);
        }
        //检查当前配件ID是否已新增
        SysPart partOpt = new SysPart();
        partOpt.setUnitId(sysPart.getUnitId());
        List<SysPart> partList = sysPartMapper.selectSysPartList(partOpt);
        if (partList.size() > 0) {
            throw new ServiceException("配件名称为：" + dictLabel + " 已存在记录，请检查再添加！");
        }

        sysPart.setCreateTime(DateUtils.getNowDate());
        return sysPartMapper.insertSysPart(sysPart);
    }



    /**
     * 修改配件
     * 
     * @param sysPart 配件
     * @return 结果
     */
    @Override
    public int updateSysPart(SysPart sysPart)
    {
        //如果没有输入别名就直接用原名称
        String dictLabel="";
        if(StringUtils.isBlank(sysPart.getPartName())){
            dictLabel = dictDataMapper.selectDictLabel("sys_part_unit_type", sysPart.getUnitId());
            sysPart.setPartName(dictLabel);
        }
        //检查当前配件ID是否已新增
        SysPart partOpt = new SysPart();
        partOpt.setUnitId(sysPart.getUnitId());
        List<SysPart> partList = sysPartMapper.selectSysPartList(partOpt);
        if (partList.size() > 1 ) {
            throw new ServiceException("配件名称为：" + dictLabel + " 已存在记录，请检查再修改！");
        }
        sysPart.setUpdateTime(DateUtils.getNowDate());
        return sysPartMapper.updateSysPart(sysPart);
    }

    /**
     * 批量删除配件
     * 
     * @param partIds 需要删除的配件主键
     * @return 结果
     */
    @Override
    public int deleteSysPartByPartIds(Long[] partIds)
    {
        return sysPartMapper.deleteSysPartByPartIds(partIds);
    }

    /**
     * 删除配件信息
     * 
     * @param partId 配件主键
     * @return 结果
     */
    @Override
    public int deleteSysPartByPartId(Long partId)
    {
        return sysPartMapper.deleteSysPartByPartId(partId);
    }
}
